package com.wang.project.remote.gatewayRemote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.openApiCommon.model.entity.InterfaceModel;
import com.wang.openApiCommon.remote.InnerInterfaceIntoService;
import com.wang.project.common.ErrorCode;
import com.wang.project.exception.BusinessException;
import com.wang.project.mapper.InterfaceInfoMapper;
import com.wang.project.model.entity.InterfaceInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Inner的方法都是注册到nacos上的远程方法
 */
@Service("InnerInterfaceIntoService")
public class InnerInterfaceInfoServiceImpl implements InnerInterfaceIntoService {

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;

    /**
     * 校验当前接口是否存在 返回当前接口信息
     *
     * @param url
     * @param method
     * @return
     */
    @Override
    public InterfaceModel getInterfaceInfo(String url, String method) {
        if (StringUtils.isAnyBlank(url, method)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url);
        queryWrapper.eq("method", method);
        InterfaceInfo interfaceInfo = interfaceInfoMapper.selectOne(queryWrapper);
        InterfaceModel interfaceModel = new InterfaceModel();
        BeanUtils.copyProperties(interfaceInfo, interfaceModel);
        return interfaceModel;
    }
}